package com.young.ums.service.impl;

import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.common.util.StringUtils;
import com.young.ums.model.User;
import com.young.ums.core.ETopic;
import com.young.ums.core.PublishSubscribeCenter;
import com.young.ums.model.Dept;
import com.young.ums.service.IDataAuthService;
import com.young.ums.service.IUmsConfigService;
import com.young.ums.service.IUserService;
import com.young.ums.util.SecureHandler;
import com.young.ums.util.UmsConstants;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 用户信息服务实现
* Created by rookie on 2018-04-03.
*/
@Service("userService")
public class UserServiceImpl implements IUserService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    @Resource(name="umsConfigService")
    IUmsConfigService configService;

    @Resource
    PublishSubscribeCenter publishSubscribeCenter;

    @Resource(name = "dataAuthService")
    IDataAuthService dataAuthService;

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    private static final String MAPPER = "com.young.ums.mapper.user";//mapper的namespace

    //获取数据列表
    @Override
    public List<User> getList(User obj) {
        //权限参数处理
        dataAuthService.handleAuthorityParameter(obj);
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<User> getPage(User obj, Page page) {
        //权限参数处理
        dataAuthService.handleAuthorityParameter(obj);
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    /**
     * 根据单位id查询用户列表
     *
     * @param companyId 单位id
     * @return
     */
    @Override
    public List<User> getListByCompanyId(String companyId) {
        if (StringUtils.isBlank(companyId)){
            return null;
        }
        User obj = new User();
        obj.setCompanyId(companyId);
        return this.getList(obj);
    }

    //查询单条
    @Override
    public User get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    @Override
    public User getUserByAccount(String account) {
        return dataAccessService.getObject(MAPPER + ".getUserByAccount", account);
    }

    //统计数量
    @Override
    public int count(User obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(User obj) {
        obj.setUpdateTime(new Date());
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //更新用户登录时间
    @Override
    public int updateLoginTime(String userId, Date loginTime) {
        User user = new User();
        user.setId(userId);
        user.setThisLoginTime(loginTime);
        user.setUpdateTime(new Date());
        return dataAccessService.update(MAPPER + ".updateLoginTime", user);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        //向发布订阅中心发布用户删除消息
        publishSubscribeCenter.publish(ETopic.USER_DEL, id);
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(User obj) {
        obj.setId(CommonUtil.getUUID());
        obj.setCreateTime(new Date());
        obj.setUpdateTime(new Date());
        obj.setTrashFlag(UmsConstants.TrashFlag.NO);//未删除
        //默认单位为NON
        if (StringUtils.isBlank(obj.getCompanyId())){
            obj.setCompanyId(UmsConstants.DEFAULT.NON_DEPT);
        }
        //设置初始密码
        if (StringUtils.isBlank(obj.getPassword())){//密码为空，设置默认密码
            obj.setPassword(configService.getUsableConfigValue(UmsConstants.UmsConfigKey.USER_DEFAULT_PASSWORD));
        }
        //密码加密
        obj.setPassword(SecureHandler.encryptPwd(obj.getPassword()));
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<User> list) {
        int i = 0;
        for(User item : list){
            i += this.insert(item);
        }
        return i;
    }

    //重置密码
    @Override
    public int resetPassword(String userId) {
        User user = new User();
        user.setId(userId);
        String defaultPwd = configService.getUsableConfigValue(UmsConstants.UmsConfigKey.USER_DEFAULT_PASSWORD);
        user.setPassword(SecureHandler.encryptPwd(defaultPwd));
        return this.update(user);
    }

    //批量重置密码
    @Override
    public int batchResetPassword(String[] userIds) {
        int i = 0;
        for(String userId : userIds){
            i += this.resetPassword(userId);
        }
        return i;
    }

}